package co.chuangdian.oflauncher.setting;

import org.json.JSONException;
import org.json.JSONObject;
import uc.PasswordType;
import uc.numberlock.Consts;
import uc.numberlock.PasswordActivity;
import uc.patternlock.CreateGesturePasswordActivity;
import co.chuangdian.oflauncher.OFActivity;
import co.chuangdian.oflauncher.R;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import cn.onekit.CALLBACK;
import cn.onekit.CONFIG;
import cn.onekit.JSON;
import cn.onekit.MESSAGE;
import cn.onekit.android.ACTIVITY;

public class LockScreen_PasswordActivity extends OFActivity implements
		OnClickListener {
	JSONObject setting;
	JSONObject lockscreen;
	ImageView setting_passwordimagive1, setting_passwordimagive2, setting_passwordimagive3;
	//4-27
	JSONObject password_number;
	
	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.setting_password);

		this.findViewById(R.id.setting_password_nonerow).setOnClickListener(this);			
		this.findViewById(R.id.setting_password_graphicrow).setOnClickListener(this);				
		this.findViewById(R.id.setting_password_numberrow).setOnClickListener(this);	
		
		setting_passwordimagive1=(ImageView) this.findViewById(R.id.setting_passwordimagive1);
		setting_passwordimagive2=(ImageView) this.findViewById(R.id.setting_passwordimagive2);
		setting_passwordimagive3=(ImageView) this.findViewById(R.id.setting_passwordimagive3);
		MESSAGE.receive("setting_password_type", null,new CALLBACK<Bundle>() {
			
			@Override
			public void run(boolean isError, Bundle result) {
				if (isError) {
					return;
				}
				loadSetting();
				showPasswordType();
			}
		});
		
		loadSetting();
		showPasswordType();
		
	
	}
	//
	void loadSetting() {
		String settingstring = (String) CONFIG.get("setting");
		setting = JSON.parse(settingstring);
		lockscreen = setting.optJSONObject("lockscreen");
		
		
	}
	//
	void showPasswordType() {
		int password_type_int = lockscreen.optInt("password_type");
		PasswordType password_type = PasswordType.values()[password_type_int];
		
		
		//显示选中的对勾图标
		switch (password_type) {
		case none:
			setting_passwordimagive1.setVisibility(View.VISIBLE);
			setting_passwordimagive2.setVisibility(View.INVISIBLE);
			setting_passwordimagive3.setVisibility(View.INVISIBLE);
			break;
		case pattern:
			setting_passwordimagive2.setVisibility(View.VISIBLE);
			setting_passwordimagive1.setVisibility(View.INVISIBLE);
			setting_passwordimagive3.setVisibility(View.INVISIBLE);
			break;
		case number:
			setting_passwordimagive3.setVisibility(View.VISIBLE);
			setting_passwordimagive1.setVisibility(View.INVISIBLE);
			setting_passwordimagive2.setVisibility(View.INVISIBLE);
			break;
		default:
			
			break;
		}
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.setting_password_nonerow:
			
			loadSetting();
			try {
				lockscreen.put("password_type", PasswordType.none.ordinal());
				setting.put("lockscreen", lockscreen);
				CONFIG.set("setting", setting);
				showPasswordType();
				MESSAGE.send("setting_password_type", null, null);
			} catch (JSONException e) {
				e.printStackTrace();
			}  
			break;
			
		case R.id.setting_password_graphicrow:
			
			
			CreateGesturePasswordActivity.gotoActivity(LockScreen_PasswordActivity.this, Consts.SETTING_PASSWORD);
			
			break;
			
		case R.id.setting_password_numberrow:
			PasswordActivity.gotoActivity(LockScreen_PasswordActivity.this,
					Consts.SETTING_PASSWORD);
			break;

		default:
			break;
		}
	}

}
